package com.example.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.provider.Settings;
import android.widget.Toast;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityManager;
import android.text.TextUtils;
import android.util.Log;

public class PermissionHandler {

    private static final int SCREEN_CAPTURE_REQUEST_CODE = 1001;

    // 打开无障碍权限设置并检查权限
    public static void requestAccessibilityPermission(Activity activity) {
        Log.d("AccessibilityCheck", "Opening accessibility settings...");
        try {
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
            Toast.makeText(activity, "请在设置中启用无障碍权限", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("AccessibilityCheck", "Failed to open accessibility settings: " + e.getMessage());
            Toast.makeText(activity, "无法打开无障碍设置: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    // 申请屏幕录制权限并检查权限
    public static void requestScreenCapturePermission(Activity activity) {
        try {
            MediaProjectionManager mediaProjectionManager =
                    (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
            if (mediaProjectionManager != null) {
                Intent intent = mediaProjectionManager.createScreenCaptureIntent();
                activity.startActivityForResult(intent, SCREEN_CAPTURE_REQUEST_CODE);
            } else {
                Toast.makeText(activity, "无法获取屏幕录制服务", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Toast.makeText(activity, "申请屏幕录制权限失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean isAccessibilityPermissionGranted(Context context) {
        Log.d("AccessibilityCheck", "Starting accessibility permission check...");
        try {
            int accessibilityEnabled = Settings.Secure.getInt(
                    context.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_ENABLED
            );
            Log.d("AccessibilityCheck", "Accessibility Enabled: " + accessibilityEnabled);
            if (accessibilityEnabled == 1) {
                String enabledServices = Settings.Secure.getString(
                        context.getContentResolver(),
                        Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
                );
                Log.d("AccessibilityCheck", "Enabled Services: " + enabledServices);
                String expectedService = "com.example.myapplication/com.example.myapplication.MyAccessibilityService"; // 确保这个路径是正确的
                Log.d("AccessibilityCheck", "Expected Service: " + expectedService);

                if (enabledServices != null && !enabledServices.isEmpty()) {
                    for (String service : enabledServices.split(":")) {
                        Log.d("AccessibilityCheck", "Checking service: " + service);
                        if (service.trim().equalsIgnoreCase(expectedService)) {
                            Log.d("AccessibilityCheck", "Accessibility service is enabled.");
                            return true;
                        }
                    }
                }
            }
        } catch (Settings.SettingNotFoundException e) {
            Log.e("AccessibilityCheck", "Settings not found: " + e.getMessage());
        }
        Log.d("AccessibilityCheck", "Accessibility service is not enabled.");
        return false;
    }

    public static boolean isScreenCapturePermissionGranted(MediaProjection mediaProjection) {
        return mediaProjection != null;
    }
}
